package com.th.supcom.pds.dispensedrug.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.Element;
import com.th.supcom.pds.common.client.model.PdsSupplyDetailModel;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid;
import com.th.supcom.pds.common.client.widget.PrescDetailGrid.ColumnConfigEnhance;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.internal.client.widget.PhamBatchCombox;

/**
 * 核对发药处方明细面板
 * 
 * @author 杨冲
 */
public class DispenseDrugRecipeDetailPanel extends ContentPanel
{

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private PhamBatchCombox m_combox;

    private PrescDetailGrid <PdsSupplyDetailModel> m_grid;

    /**
     * 构造函数
     */
    public DispenseDrugRecipeDetailPanel ()
    {
        initUI ();
    }

    private CellEditor createCellEditor ()
    {
        return new CellEditor (m_combox)
        {

            @Override
            public Object postProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }
                return ((ModelData) value).get ("phamBatchNumber");
            }

            @Override
            public Object preProcessValue (final Object value)
            {
                if (null == value)
                {
                    return value;
                }

                final BaseModelData data = new BaseModelData ();
                data.set ("phamBatchNumber", value);
                return data;
            }

            @Override
            public void startEdit (final Element el, final Object value)
            {
                if (null == m_grid.getGrid ().getSelectionModel ().getSelectedItem ().getPhamBatchNumber ())
                {
                    super.hide ();
                    return;
                }
                else
                {
                    PdsSupplyDetailModel model = m_grid.getGrid ().getSelectionModel ().getSelectedItem ();
                    model.setPerformedBy (ClientUtils.getOrgModel().getOrgCode()); 
                    m_combox.loadData (model);
                    super.startEdit (el, value);
                }
            }
        };
    }

    private void createGrid ()
    {
        m_grid = new PrescDetailGrid <PdsSupplyDetailModel> (false, new ColumnConfigEnhance ()
        {

            @Override
            public void enhance (final List <ColumnConfig> configs)
            {
                final ColumnConfig column = new ColumnConfig ();
                column.setId ("phamBatchNumber");
                column.setHeader ("批次");
                column.setWidth (80);
                column.setRenderer (BatchNumberRenderer ());
                m_combox = new PhamBatchCombox (80);
                column.setEditor (createCellEditor ());
                configs.add (column);
            }

        });
        add (m_grid);
    }

    private GridCellRenderer <PdsSupplyDetailModel> BatchNumberRenderer ()
    {
        return new GridCellRenderer <PdsSupplyDetailModel> ()
        {
            @Override
            public Object render (final PdsSupplyDetailModel model, final String property, final ColumnData config, final int rowIndex, final int colIndex,
                                  final ListStore <PdsSupplyDetailModel> store, final Grid <PdsSupplyDetailModel> grid)
            {
                final String modelname = model.getPhamBatchNumber ();
                String date = null;
                if (model.getPhamBatchNumber () != null)
                {
                    date = "<div style='background-color:#8DB6CD;width:70px;height:14px;float:center;'>" + modelname + "</div>";
                }
                return date;
            }
        };
    }

    /**
     * 获取Grid
     * 
     * @return
     */
    public PrescDetailGrid <PdsSupplyDetailModel> getGrid ()
    {
        return m_grid;
    }

    /**
     * 获取Combox
     * 
     * @return
     */
    public PhamBatchCombox getPhamBatchCombox ()
    {
        return m_combox;
    }

    /**
     * 初始化UI
     */
    private void initUI ()
    {
        this.setHeading ("处方明细");
        this.setLayout (new FitLayout ());

        createGrid ();
    }
}
